<template>
  <div class="content">
    <el-form ref="form" :model="form" label-width="80px" :inline="true">

      <el-form-item label="单位名称" prop="deptId">
        <el-select
          clearable
          v-model="form.deptId"
          @change="gradeList(form.deptId)"
          placeholder="请选择所属学校"
          size="mini"
          style="width:150px"
        >
          <el-option :label="item.name" :value="item.id" v-for="item in schoolList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select
          clearable
          v-model="form.glassId"
          @change="classList(form.glassId)"
          placeholder="请选择年级"
          size="mini"
          style="width:150px"
        >
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item,index) in gradeData"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select @change="common()" clearable v-model="form.classId" placeholder="请选择班级" size="mini" style="width:150px">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="(item,index) in classData"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>

      <!--
      <el-button type="primary" size="mini">创建学生照片</el-button>
      <el-button type="primary" size="mini">导出学生信息</el-button>-->
    </el-form>

    <!-- <el-divider></el-divider> -->
    <template>
      <div style="height:200px; overflow:auto">
      <el-table :data="tableData" v-loading="loading" size="mini" style="width: 100%;margin-top:30px">

        <el-table-column prop="id" fixed label="学生ID" width="150"></el-table-column>
        <el-table-column prop="name" fixed label="姓名"></el-table-column>
        <el-table-column prop="balance" label="余额"></el-table-column>
        <el-table-column prop="idNo" width="150" label="身份证号"></el-table-column>

        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.status==1?"在校":'毕业'}}</span>
          </template>
        </el-table-column>


      </el-table>
      </div>
    </template>
    <el-row>
      <el-col :span="24">
        <el-pagination
          style="float:right;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 15, 20,50,100]"
          :page-size="everyPageNum"
          layout="total, sizes,prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <div class="grid-content bg-purple-dark" style="font-size:13px;float:right;margin:26px 0 0 10px">合计金额：<span style="color:rgb(20, 136, 154);font-weight:bold">{{sumData}}</span></div>
      </el-col>
    </el-row>
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20,50,100]"
      :page-size="everyPageNum"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination> -->
    <!--新增编辑弹框-->
    <!-- <el-row>
      <el-col :span="24"><div class="grid-content bg-purple-dark">合计金额：111</div></el-col>
    </el-row> -->
    <el-form
      :model="addForm"
      :rules="addForm"
      ref="addForm"
      label-width="130px"
      class="demo-addForm"
      size="mini"
    >
      <el-row style="padding-top: 20px">
        <el-form-item label="退款人数：" prop="deptId"><el-col :span="12">{{total}}</el-col></el-form-item>
      </el-row>
      <el-row style="padding-top: 20px">
        <el-form-item label="退款金额：" prop="deptId"><el-col :span="12">{{sumData}}</el-col></el-form-item>
      </el-row>
     <el-row style="padding-top: 20px">
        <el-col :span="12">
          <el-form-item label="退款原因" prop="reason">
            <el-select
              v-model="addForm.reason"
              placeholder="退款原因"

            >
              <el-option
                :label="item.value"
                :value="item.value"
                v-for="item in reasonList"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row style="padding-top: 20px">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input class="title" type="textarea" v-model="form.remark" placeholder="请输入备注" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item style="padding-top: 20px">
        <el-button :disabled="btnChangeEnable" type="primary" @click="submitForm(0)">{{$t("action.save")}}</el-button>
        <el-button :disabled="btnChangeEnable" type="success" @click="submitForm(1)">{{$t("action.submit")}}</el-button>
        <el-button :disabled="btnChangeEnable" type="danger"  @click="resetForm()">{{$t("action.reset")}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import editStudent from "@/components/StudentManagement/edit";

export default {
  name: "StudentManagement",
  components: {
    editStudent
  },
  data() {
    return {
      url: this.$Config.url,
      orgName: '',
      className: '',
      gladName: '',
      showEdit: false, //显示编辑弹框
      updateStatus: 1, //默认是新增
      loading: false,
      id: "", //学校id
      deptId: "", //学校id
      classId: "", //学校id
      total: 1, //总页数
      everyPageNum: 10, //每页多少条
      currentPage: 1, //当前页
      btnChangeEnable: false,
      form: {
        statusChecked: "",
        statusValue: "",
        glassId: "",
        classId: "",
        status: "1",
        deptId: "",
        remark: "",
        phone:"",
        gradeId: ""
      },
      addForm: {
        reason: ""
      },
      tableData: [],
      gradeData: [], //年级列表
      classData: [], //班级列表
      schoolList: [], //学校数据
      reasonList: [], //学校数据
      requestHeader: {
        "x-baie-admin-token": Cookies.get("token")
      },
      disableSchool: false,
      sumData: ''
    };
  },
  mounted() {
    this.common();
    this.getSchool();
    this.getReasonList();
    const userInfo = JSON.parse(localStorage.getItem("userInfo")).deptId;
    if (userInfo == null || userInfo == "") {
      this.disableSchool = false;
    } else {
      this.form.deptId = userInfo;
      this.disableSchool = true;
      this.gradeList(userInfo);
    }
  },
  methods: {
    getSchool() {
      const that = this;
      this.$axios
        .get(that.url + "/admin/dept/list", {
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.schoolList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    resetData(id = 1) {
      this.resetForm();//先清空
      const that = this;
      if (id) {
        this.id = id;
      }
      this.$axios
        .get(that.url + "/admin/refundBatch/get/{id}", {
          params: {
            id
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.status == 200) {
            let data = res.data.data;
            that.orgName = data.orgName;

            that.form.gradeId = data.gradeId;
            that.form.glassId = data.gradeId;
            that.form.deptId = data.deptId;
            that.addForm.reason = data.reason;
            that.form.remark = data.remark;
            that.gradeList(data.deptId);

            that.id = data.id;
            that.classId = data.classId
            that.classList(data.gradeId);
            that.form.classId = data.classId;
            that.common()
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    resetForm(){
      const that = this;
        that.orgName = '';
        that.form.classId = '';
        that.form.glassId = '';
        that.form.deptId = '';
        that.addForm.reason = '';
        that.form.remark = '';
        this.common()
    },
    submitForm(status) {
      const that = this;
      if(that.form.classId == ''){
        that.$message({
          message: '请选择班级',
          type: "warning"
        });
        return;
      }
      if(that.addForm.reason == ''){
        that.$message({
          message: '请选择退款原因',
          type: "warning"
        });
        return;
      }

      const data = {
        id: that.id,
        orgName: that.orgName,
        classId: that.form.classId,
        deptId: that.form.deptId,
        reason: that.addForm.reason,
        remark: that.form.remark,
        status: status
      };
      that.btnChangeEnable = true;
      this.$axios
        .post(
          that.url + "/admin/refundBatch/saveOrUpdate",
          that.$qs.stringify(data),
          {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          }
        )
        .then(function(res) {
          if (res.status == 200) {
            if(res.data.code==200){
              if (that.msg == 1) {
                that.$message({
                  message: "提交成功",
                  type: "success"
                });
              } else {
                that.$message({
                  message:"提交成功",
                  type: "success"
                });
              }
              that.btnChangeEnable = false;
              that.$emit("hideEdit", false);
            }else{
              that.btnChangeEnable = false;
              that.$message({
                message: res.data.msg,
                type: "warning"
              });
            }
          } else {
            that.btnChangeEnable = false;
            that.$message({
              message: res.statusText,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
          that.btnChangeEnable = false;
        });
    },


    getReasonList(){
      let param = {
        pageNum:1,
        pageSize:10,
        params:[{
          name:'label',
          value:'退款原因'
        }]
      }
      this.$api.dict.findPage(param).then((res) => {
        this.reasonList = res.data.content
      })
    },
    gradeList(id) {
      const that = this;
      this.$axios
        .get(that.url + "/admin/grade/list", {
          params: {
            deptId: id
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.gradeData = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    classList(id) {
      const that = this;
      that.form.classId = "";
      let gradeData = that.gradeData
      for(let i=0;i<gradeData.length;i++){
        if(gradeData[i].id == id){
          that.gladName = gradeData[i].name
          break;
        }
      }
      this.$axios
        .get(that.url + "/admin/class/listBranck", {
          params: {
            gradeId: id,
            classId: that.classId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.classData = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    search() {
      this.common(this.currentPage, this.everyPageNum);
    },

    handleSizeChange(val) {
      //   每页多少条
      this.everyPageNum = val;
      this.common(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.common(val, this.everyPageNum);
    },
    hideEditStatus(status) {
      this.showEdit = false;
      this.common(this.currentPage, this.everyPageNum);
    },
    common(pageNo = 1, pageSize = 10) {
      const that = this;
      that.loading = true;
      let classId = that.form.classId;
      if(classId == ''){
        classId = -1;
      }else{
        let classData = that.classData
        for(let i=0;i<classData.length;i++){

          if(classData[i].id == classId){
            that.className = classData[i].name
            break;
          }
        }
        that.orgName = that.gladName + that.className;
      }
      this.$axios.get(that.url + "/admin/student/page", {
          params: {
            pageNo,
            pageSize,
            name: that.form.statusChecked == 1 ? that.form.statusValue : "",
            studentNo:
              that.form.statusChecked == 2 ? that.form.statusValue : "",
            idNo: that.form.statusChecked == 3 ? that.form.statusValue : "",
            status: that.form.status,
            gradeId: that.form.glassId,
            classId: classId,
            phone:that.form.phone
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          that.loading = false;
          if (res.data.code == 200) {
            that.tableData = res.data.data.records;
            that.total = res.data.data.total;
            that.sumData = res.data.sum;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          that.loading = false;
          console.log(error);
        });
    },
    handleClick(row) {
      console.log(row);
    }
  }
};
</script>

<style scoped lang="scss">
.upload {
  display: inline-block;
}
.el-pagination {
  float: right;
  margin: 20px 10px;
}
.button_list {
  margin-bottom: 10px;
}

.content {
  text-align: left;
}

.el-divider {
  margin: 10px 0;
}

.btn {
  margin-top: 6px;
}

.el-form-item {
  margin-bottom: 0;
}
</style>
